﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;
public class DownLoadManager{


    public static string LoadLocalTxt(string path)
    {
        string str = "";
        //var filePath = Path.Combine(url, fileName);
        FileInfo fileInfo = new FileInfo(path);
        if (fileInfo.Exists)
        {
            StreamReader sr = new StreamReader(path);
            str = sr.ReadToEnd();
        }
        return str;
    }

	public static void DownLoad(string path,Action<WWW> callback)
    {
        ResManager.Instance.StartCoroutine(_DownLoad(path, callback));
    }

    private static IEnumerator _DownLoad(string path, Action<WWW> callback)
    {
        //path = "file:///" + Application.persistentDataPath + "/Res/Lua_version.txt";
        WWW www = new WWW(path);

        yield return www;

        if(string.IsNullOrEmpty(www.error))
        {
            if (callback != null)
                callback(www);
        }
        else
        {
            Loger.Error("DownLoadManager", path + "Error is :" + www.error);
            if (callback != null)
                callback(null);
        }
    }


    /// <summary>
    /// 下载资源并保存到指定目录
    /// </summary>
    /// <param name="path"></param>
    /// <param name="savePath"></param>
    /// <param name="callback"></param>
    public static void DownLoadToSave(string path,string savePath,Action callback)
    {
        ResManager.Instance.StartCoroutine(_DownLoadToSave(path, savePath, callback));
    }

    private static IEnumerator _DownLoadToSave(string path, string savePath, Action callback)
    {
        WWW www = new WWW(path);

        yield return www;
    }
}
